package heap

import "gvm/classfile"

// 符号引用
type SymRef struct {
	constantPool *ConstantPool
	className    string
	class        *Klass
}

// 解析符号引用
func (self *SymRef) ResolveClass() *Klass {
	if self.class == nil {
		self.resolveClass()
	}
	return self.class
}

func (self *SymRef) resolveClass() {
	c := self.constantPool.class.classloader.LoadClass(self.className)
	if !c.isAccessibleTo(self.constantPool.class) {
		panic("java.lang.IllegalAccessError")
	}
	self.class = c
}

type ClassRef struct {
	SymRef
}

func NewClassRef(cp *ConstantPool, classInfo *classfile.Constant_Class) *ClassRef {
	ref := &ClassRef{}
	ref.constantPool = cp
	ref.className = classInfo.GetClassName()
	return ref
}

type MemberRef struct {
	SymRef
	name       string
	descriptor string
}

func (self *MemberRef) copyFiled(ref *classfile.Constant_Field_Ref) {
	self.className = ref.GetClassName()
	self.name, self.descriptor = ref.GetNameAndType()
}

func (self *MemberRef) copyMethod(ref *classfile.Constant_Method_Ref) {
	self.className = ref.GetClassName()
	self.name, self.descriptor = ref.GetNameAndType()
}
